<?php
class Search_Form_JobRefineForm extends Ac_Form
{
    private $settings;


    protected $elementDecorators = array(
        'ViewHelper',
        'Errors',
        array('Label', array()),
        array(array('row' => 'HtmlTag'), array('tag' => 'div', 'class'=>'text')),
    );

    protected $elementSelectDecorators = array(
        'ViewHelper',
        'Errors',
        array('Label', array()),
        array(array('row' => 'HtmlTag'), array('tag' => 'div', 'class'=>'select')),
    );

    protected $formDecorators = array(
        'FormElements',
        array('Description', array('placement' => 'prepend')),
        'Form',

    );

    protected $elementCheckBoxDecorators = array(
        'ViewHelper',
        'Errors',
        array('Label', array('placement' => 'APPEND')),
        array(array('row' => 'HtmlTag'), array('tag' => 'div', 'class'=>'checkbox')),
    );
    

    protected $elementMultiCheckBoxDecorators = array(
        'ViewHelper',
        'Errors',
        array('Label', array('tag' => 'span', 'class' => 'multicheckbox-label')),
    );        

    protected $displayGroupDecorators = array(
        'FormElements',
    );


    protected $subFormDecorators = array(
        'FormElements',
        array('Description', array('placement' => 'prepend')),
    );


    protected $elementButtonDecorators = array(
        'ViewHelper',
        'Errors',
    );

    public function __construct($options = array())
    {
        $this->settings = (object) (!empty($options['settings']) ? $options['settings'] : array());
        parent::__construct($options);
    }

    protected function second()
    {
        $form1 = $this->getNewSubForm();

        $deco = array_merge($this->elementMultiCheckBoxDecorators, array(array(array('row' => 'HtmlTag'), array('tag' => 'div', 'class'=>'checkbox household_and_cleaning'))) );
        // Employment Type
        $form1->addElement('multiCheckbox', 'key_search_tool_employmenttype', array(
            'filters'    => array('StringTrim'),
            'validators' => array(
                //'EmailAddress',
            ),
            'decorators'   => $deco,
            'multiOptions' => Model_Listing_Job::getEmploymentType(),
            'required'     => false,
            'label'        => 'Employment Type',
            'value'        => $this->getSetting('employmenttype'),
        ));

        $deco = array_merge($this->elementMultiCheckBoxDecorators, array(array(array('row' => 'HtmlTag'), array('tag' => 'div', 'class'=>'checkbox household_and_cleaning'))) );        
        // Job Location
        $form1->addElement('multiCheckbox', 'key_search_tool_joblocation', array(
            'filters'    => array('StringTrim'),
            'validators' => array(
                //'EmailAddress',
            ),
            'decorators'   => $deco,
            'multiOptions' => Model_Listing_Job::getJobLocation(),
            'required'     => false,
            'label'        => 'Job Location',
            'value'        => $this->getSetting('joblocation'),
        ));

        $deco = array_merge($this->elementMultiCheckBoxDecorators, array(array(array('row' => 'HtmlTag'), array('tag' => 'div', 'class'=>'checkbox household_and_cleaning'))) );        
        // Career Level
        $form1->addElement('multiCheckbox', 'key_search_tool_careerlevel', array(
            'filters'    => array('StringTrim'),
            'validators' => array(
                //'EmailAddress',
            ),
            'decorators'   => $deco,
            'multiOptions' => Model_Listing_Job::getCareerLevel(),
            'required'     => false,
            'label'        => 'Career Level',
            'value'        => $this->getSetting('careerlevel'),
        ));

        // Salary
        /*
        $form1->addElement('jqslider', 'key_search_tool_salary', array(
                    'jQueryParams' => array(
                        'min'      => $this->getSetting('salaryrange_min') !== null ? $this->getSetting('salaryrange_min') : Model_Listing_Job::SALARY_MIN,
                        'max'      => $this->getSetting('salaryrange_max') !== null ? $this->getSetting('salaryrange_max') : Model_Listing_Job::SALARY_MAX,
                        'values'   => $this->getSetting('salaryrange_start') !== null ? $this->getSetting('salaryrange_start') : array(Model_Listing_Job::SALARY_MIN,Model_Listing_Job::SALARY_MAX),
                        'currency' => '$HKD',
                        'range'    => true,
                        'step'     => 1,
                        'change'   => 'refineSearch()',
                        ),
                    'label'       => 'Salary (,000 HKD$)',
                    'decorators' => array(
                            'Errors',
                            'UiWidgetElement',
                            array('Label', array('escape' => false)),
                            array(array('row' => 'HtmlTag'), array('tag' => 'div', 'class' => 'slide')),
                    )
                )
        );
        */
        
        $form1->addElement('textRange', 'salary', array(
                    'label'       => 'Salary (HKD)',
                    'value'       => $this->getSetting('salary_start') !== null ? $this->getSetting('salary_start') : array(),
                    'class'       => array('class' => 'text-range'),
                    'decorators'  => array(
                            'Errors',
                            'ViewHelper',
                            array('Label', array('escape' => false)),
                            array(array('row' => 'HtmlTag'), array('tag' => 'div', 'class' => 'text')),
                    )
                )
        );        
        
        // Year Experience
        $form1->addElement('jqslider', 'key_search_tool_yearexperience', array(
                    'jQueryParams' => array(
                        'min'      => $this->getSetting('yearexperience_min') !== null ? $this->getSetting('yearexperience_min') : Model_Listing_Job::YEAR_EXPERIENCE_MIN,
                        'max'      => $this->getSetting('yearexperience_max') !== null ? $this->getSetting('yearexperience_max') : Model_Listing_Job::YEAR_EXPERIENCE_MAX,
                        'values'   => $this->getSetting('yearexperience_start') !== null ? $this->getSetting('yearexperience_start') : array(Model_Listing_Job::YEAR_EXPERIENCE_MIN, Model_Listing_Job::YEAR_EXPERIENCE_MAX),
                        'currency' => 'Year',
                        'range'    => true,
                        'step'     => 1,
                        'change'   => 'refineSearch()',
                        ),
                    'label'       => 'Years of Experience',
                    'decorators' => array(
                            'Errors',
                            'UiWidgetElement',
                            array('Label', array('escape' => false)),
                            array(array('row' => 'HtmlTag'), array('tag' => 'div', 'class' => 'slide')),
                    )
                )
        );        

        $deco = array_merge($this->elementMultiCheckBoxDecorators, array(array(array('row' => 'HtmlTag'), array('tag' => 'div', 'class'=>'checkbox household_and_cleaning'))) );        
        // Education
        $form1->addElement('multiCheckbox', 'key_search_tool_education', array(
            'filters'    => array('StringTrim'),
            'validators' => array(
                //'EmailAddress',
            ),
            'decorators'   => $deco,
            'multiOptions' => Model_Listing_Job::getEducation(),
            'required'     => false,
            'label'        => 'Required Education',
            'value'        => $this->getSetting('education'),
        ));

        $deco = array_merge($this->elementMultiCheckBoxDecorators, array(array(array('row' => 'HtmlTag'), array('tag' => 'div', 'class'=>'checkbox household_and_cleaning'))) );        
        // Resident Status
        $form1->addElement('multiCheckbox', 'key_search_tool_residentstatus', array(
            'filters'    => array('StringTrim'),
            'validators' => array(
                //'EmailAddress',
            ),
            'decorators'   => $deco,
            'multiOptions' => Model_Listing_Job::getResidentStatus(),
            'required'     => false,
            'label'        => 'Resident Status',
            'value'        => $this->getSetting('residentstatus'),
        ));

    
        // Keyword Button
        $form1->addElement('text', 'keyword', array(
            'required'    => false,
            'ignore'      => true,
            'label'       => 'Keyword',
            'value'       => $this->getSetting('keyword'),
            'decorators'  => $this->elementDecorators,
        ));        
    
        $form1->addDecorator('fieldset', array('class' => 'key_search'));
        $form1->setLegend('Find your job');
        $this->addSubForm($form1, 'jobs');
    }
    
    protected function button()
    {
        $form2 = $this->getNewSubForm();
        $form2->setDecorators(
                array('FormElements')
        );
        // Submit Button
        $form2->addElement('button', 'v', array(
            'required'   => false,
            'ignore'     => true,
            'type'       => 'submit',
            'label'      => 'Go!',
            'decorators'  => array(
                    'Errors',
                    'ViewHelper',
                    array(array('row' => 'HtmlTag'), array('tag' => 'div', 'class' => 'button')),
            )
        ));

        $this->addSubForm($form2, 'key_search_tools2');
    }    

    public function init()
    {
        parent::init();

        $this->setSubFormDecorators($this->subFormDecorators);
        $this->setDisplayGroupDecorators($this->displayGroupDecorators);

        $this->setMethod('GET');
        $this->setName('RefineSearch');

        /*
        $element = new Zend_Form_Element_Hidden('keyword');
        if (!empty($_GET['keyword'])) {
            $element->setValue($_GET['keyword']);
        }
        $element->setDecorators(array(
                'ViewHelper'
            )
        );
        $this->addElement($element);
        */
        
        $element = new Zend_Form_Element_Hidden('small_category_list');
        if (Zend_Registry::isRegistered('category_list_selected')) {
            $selected = Zend_Registry::get('category_list_selected');
            $element->setValue($selected);
        } elseif (!empty($_GET['small_category_list'])) {
            $selected = $_GET['small_category_list'];
            $element->setValue($selected);
        }
        $element->setDecorators(array(
                'ViewHelper'
            )
        );
        $this->addElement($element);

        $this->second();
        $this->button();
    }

    function populate(array $values)
    {
        //Keyword
        if (isset($values['jobs']['keyword'])) {
            $this->settings->keyword = $values['jobs']['keyword'];
        }

        //Employment Type
        if (isset($values['jobs']['key_search_tool_employmenttype'])) {
            $this->settings->employmenttype = $values['jobs']['key_search_tool_employmenttype'];
        }

        //Job Location
        if (isset($values['jobs']['key_search_tool_joblocation'])) {
            $this->settings->joblocation = $values['jobs']['key_search_tool_joblocation'];
        }

        //Resident status
        if (isset($values['jobs']['key_search_tool_residentstatus'])) {
            $this->settings->residentstatus = $values['jobs']['key_search_tool_residentstatus'];
        }

        //Education
        if (isset($values['jobs']['key_search_tool_education'])) {
            $this->settings->education = $values['jobs']['key_search_tool_education'];
        }
        
        //Career Level
        if (isset($values['jobs']['key_search_tool_careerlevel'])) {
            $this->settings->careerlevel = $values['jobs']['key_search_tool_careerlevel'];
        }

        //Salary
        if (isset($values['jobs-key_search_tool_salary']) && isset($values['jobs-key_search_tool_salary-1'])) {
            $this->settings->salaryrange_start = array($values['jobs-key_search_tool_salary'], $values['jobs-key_search_tool_salary-1']);
        }
        if (isset($values['salary']) && isset($values['salary-1'])) {
            $this->settings->salaryrange_start = array($values['salary'], $values['salary-1']);
        }        
        
        //Career Level
        if (isset($values['jobs']['key_search_tool_careerlevel'])) {
            $this->settings->careerlevel = $values['jobs']['key_search_tool_careerlevel'];
        }

        //Year Experience
        if (isset($values['jobs-key_search_tool_yearexperience']) && isset($values['jobs-key_search_tool_yearexperience-1'])) {
            $this->settings->yearexperience_start = array($values['jobs-key_search_tool_yearexperience'], $values['jobs-key_search_tool_yearexperience-1']);
        }        
    }

    private function getNewSubForm()
    {
        $form = new Ac_SubForm();
        $form->setDecorators($this->subFormDecorators);
        $form->setDisplayGroupDecorators($this->displayGroupDecorators);
        return $form;
    }

    private function getSetting($name)
    {
        if (!empty($this->settings->$name)) {
            return $this->settings->$name;
        }
        return null;
    }

    public function __toString()
    {
        $str = '
            $j("#jobs-key_search_tool_jobcategory").change(function (event) {
                event.stopImmediatePropagation();
                $j.get("/search/get-attribute", { c:"job1", f:"model", id: $j("#jobs-key_search_tool_jobcategory").val() },
                    function(data) {
                        $j("#jobs-key_search_tool_jobfunction").empty();
                        $j.each(data, function (index, value) {
                            $(document.createElement("option"))
                                .html(value)
                                .attr("value", index)
                                .appendTo($("#jobs-key_search_tool_jobfunction"));
                        });
                    });
           });
        ';
        $this->view->JQuery()->addJavascript($str);
        return parent::__toString();
    }
}